package com.lysj.bhp.util.aop;


import com.lysj.bhp.manager.base.ManagerContent;
import com.lysj.bhp.manager.domain.Language;
import com.lysj.bhp.util.web.Resp;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zk
 * @description
 * @create 2018-06-02 16:53
 **/
@Aspect
@Component
public class Internationalization extends ManagerContent {

    @Pointcut("execution(* com.lysj.bhp.api.controller.*..*(..)) && !execution(* com.lysj.bhp.api.controller.*.InitBinder*(..))")
    public void controllerAspect(){}

    @AfterReturning(returning = "rvt", pointcut = "controllerAspect()")
    public void after(JoinPoint joinPoint, Object rvt) {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        String t = (String)request.getParameter("language");
        Resp resp = (Resp) rvt;
        //如果前端需要返回的是英文提示
        if("EN".equals(t)&&null!=resp){
            List<Language> languageList = languageRepository.findByChinese(resp.getMsg());
            if (languageList.size() != 0){
                resp.setMsg(languageList.get(0).getEnglish());
            }
        }
    }
}
